package com.cmccgz.udacs_landlord_webapp.main.peoplemanager.presenter;

import com.cmccgz.udacs_landlord_webapp.base.ApiService;
import com.cmccgz.udacs_landlord_webapp.bean.RenterBean;
import com.cmccgz.udacs_landlord_webapp.bean.ResultBean;
import com.cmccgz.udacs_landlord_webapp.main.peoplemanager.contract.PeopleListContract;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Denis
 * @ClassName: PeoplePresenter
 * @Description: 租客列表页
 * @date 2017/6/7 14:37
 */

public class PeopleListPresenter implements PeopleListContract.Presenter {
    private PeopleListContract.View view;
    public PeopleListPresenter(PeopleListContract.View view) {
        this.view = view;
        view.setPresenter(this);
    }

    @Override
    public void initData(String mobile, String q_word, String o_cond, final String page) {
        Subscriber<ResultBean<List<RenterBean>>> subscriber = new Subscriber<ResultBean<List<RenterBean>>>() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                view.refreshFail("网络异常！");
            }

            @Override
            public void onNext(ResultBean<List<RenterBean>> listResultBean) {
                if (listResultBean.rsmsg.rscode.equals("0")) {
                    if (page.equals("1")) {
                        view.getData(listResultBean.resp);
                    } else {
                        view.loadmoreSuccess(listResultBean.resp);
                    }
                } else {
                    view.refreshFail("加载失败");
                }
            }
        };
        ApiService.getUserList(mobile, q_word, o_cond, page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    @Override
    public void start() {

    }

}
